﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Xml;
using IE=Interop.SHDocVw;
using mshtml;
using HAP= HtmlAgilityPack;
using System.IO;
using System.Text.RegularExpressions;
using TaobaoPick.util;

namespace TaobaoPick.taboao
{
    public class TaobaoQueryParser
    {
        Regex dregx = new Regex(@"\d+");       
        String defaultTempPath ;

        public String DefaultTempPath
        {
            get
            {
                return defaultTempPath;
            }
            set
            {
                defaultTempPath = value;
                if (defaultTempPath.EndsWith("\\") == false)
                    defaultTempPath += "\\";
                System.IO.Directory.CreateDirectory(defaultTempPath);
            }
        }

        XmlDocument curDoc=null;
        public TaobaoQueryParser()
        {
            DefaultTempPath = "d:\\taobaotemp";
        }
        protected void createCDataSection(XmlElement parent,String data)
        {
            XmlCDataSection s=curDoc.CreateCDataSection(data);
            parent.AppendChild(s);
        }
        protected XmlElement createNote(String name,XmlElement parent)
        {
            
            if (curDoc == null)
                curDoc = new XmlDocument();
            
            XmlElement ele = curDoc.CreateElement(name);
            if (parent == null)
                curDoc.AppendChild(ele);
            else
                parent.AppendChild(ele);
            return ele;
        }

        protected void processSummary(HAP.HtmlNode n,XmlElement parent)
        {

            HAP.HtmlNode sa = n.SelectSingleNode(n.XPath + "/a");
            String title = sa.GetAttributeValue("title", "");
            XmlElement ele = createNote("title", parent);
            if ("".Equals(title))
            {
                ele.InnerText = sa.OuterHtml;
            }
            else
            {
                ele.InnerText = title;
            }
            String href = sa.GetAttributeValue("href", "").Replace("&amp;", "&");                
            

            ele = createNote("logUrl", parent);
            ele.InnerText = href;

            if (href.IndexOf("http://item.taobao.com/item.htm?id=") >= 0)
            {
                ele = createNote("id", parent);
                ele.InnerText = href.Substring("http://item.taobao.com/item.htm?id=".Length);
                ele = createNote("itemUrl", parent);
                ele.InnerText = href;
            }    
        }
        protected void processPhoto(HAP.HtmlNode n,XmlElement parent)
        {
            
            //HAP.HtmlNode img = n.SelectSingleNode("img");
            IEnumerable<HAP.HtmlNode> items=n.Descendants("img");

            foreach (HAP.HtmlNode ii in items)
            {
                String imgsrc = ii.GetAttributeValue("src", "");
                if ("".Equals(imgsrc))
                imgsrc = ii.GetAttributeValue("data-ks-lazyload", "");

                if ("".Equals(imgsrc))
                {
                    //沒圖~~
                    createNote("photoError", parent);
                }
                else
                {
                    XmlElement ele = createNote("img", parent);
                    ele.InnerText = imgsrc;
                    //processImg...
                    String localfile = TaobaoPick.cache.IECache.findCacheEntry(imgsrc);
                        if(localfile!=null)
                        {
                            FileInfo fi = new FileInfo(localfile);
                            if (fi.Exists)
                            {
                                String newfileName = defaultTempPath + NewGUID.create("imgs").ToString() + fi.Extension;
                                File.Copy(localfile, newfileName);
                                ele = createNote("localImgFile", parent);
                                ele.InnerText = newfileName;
                            }
                        }
                }


                break;
            }




            //can透過舉報寶貝 來取得 id

            HAP.HtmlNode antispam = n.SelectSingleNode(n.XPath + "/a[@class='antispam']");
            if (antispam != null)
            {
                String v=antispam.GetAttributeValue("href", "").Replace("&amp;", "&");

                Hashtable ht=HTMLHelpers.parseParameter(v);
                if (ht.ContainsKey("auction_num_id"))
                {
                    String id = (String)ht["auction_num_id"];
                    XmlElement ele = createNote("id_by_antispam", parent);
                    ele.InnerText = id;
                }
            }
        }
        protected void processAttribute(HAP.HtmlNode n, XmlElement parent)
        {
            String xpath = n.XPath;
            HAP.HtmlNode node;
            node = n.SelectSingleNode(xpath + "/li[@class='sale']");
            //dump(node.InnerText);
            String t = node.InnerText;
            

            XmlElement ele ;
            ele= createNote("recentSale", parent);
            {
                String[] tss=t.Split('\t');
                if (tss.Length > 0)
                {
                        //因為不能用中文,編碼問題,因此改用抓數字
                        //可是又碰到 萬筆的問題

                    String targetS = tss[0].Trim();
                    const String recs = "最近成交";
                    int st = recs.Length;
                    int pre = targetS.IndexOf(recs);
                    if (pre >= 0)
                    {
                        int post = targetS.IndexOf("万笔");
                        if (post < 0)
                            post = targetS.IndexOf("萬筆");
                        if (post > 0)
                        {
                            targetS = targetS.Substring(pre + st, post - (pre + st));
                            float v = 0;
                            float.TryParse(targetS, out  v);
                            v = v * 10000;
                            int r = (int)v;
                            ele.InnerText =r.ToString();
                        }
                        else //沒有萬筆
                        {
                            Match m = dregx.Match(targetS);
                            if (m.Success)
                            {
                                ele.InnerText = m.Groups[0].Value;
                            }
                        }
                    }
                    
                }
                //String ps = t.Substring(p + "最近成交".Length, t.IndexOf("筆", p) - (p + "最近成交".Length));
            }

            
            //如果沒有評價就有可能˙..
            node = n.SelectSingleNode(xpath + "/li[@class='sale']/a[@class='reviews']"); //text=評價
            ele = createNote("reviews", parent);
            ele.InnerText = "0";
            if (node != null)
            {
                t = node.InnerHtml;
                Match m = dregx.Match(t);
                if (m.Success)
                {
                    ele.InnerText = m.Groups[0].Value;
                }
                node.GetAttributeValue("href", "");//評價url
            }
            
            
            
            node = n.SelectSingleNode(xpath + "/li[@class='place']");
           // dump(node.InnerHtml);
            ele = createNote("place", parent);

            t = node.InnerHtml;
            if (t.IndexOf("<") > 0)
            {
                t = t.Substring(0, t.IndexOf("<"));
            }
            ele.InnerText = t;

            node = n.SelectSingleNode(xpath + "/li[@class='price']/em");
            //dump(node.InnerText);
            ele = createNote("price", parent);
            ele.InnerText = node.InnerText.Trim();
            //地區..

        }
        /********************************************************************************************
         * 
         * 
         * ******************************************************************************************/
        protected void processIconArea(HAP.HtmlNode n, XmlElement parent)
        {
            HAP.HtmlNode node=n.SelectSingleNode(n.XPath + "/div[@class='icon-area']");
            XmlElement ele=createNote("isTmall", parent);
            if (node == null)
            {
                ele.InnerText = "false";
            }
            else
            {
                if(node.InnerHtml.IndexOf("http://www.tmall.com")>=0)
                    ele.InnerText = "true";
                else
                    ele.InnerText = "false";
                
            }

        }
        /********************************************************************************************
         * 
         * 
         * ******************************************************************************************/
        protected void processSeller(HAP.HtmlNode n, XmlElement parent)
        {
            //dump(n.OuterHtml);
            HAP.HtmlNode node = n.SelectSingleNode(n.XPath + "/a");
            XmlElement ele = createNote("storeName", parent);
            if (node == null)
            {
                ele.InnerText = "No StoreName";
                ele = createNote("storeURLClick", parent);
                createCDataSection(ele,"");            

            }
            else
            {
                ele.InnerText = node.InnerHtml;
                ele = createNote("storeURLClick", parent);
                String h=node.GetAttributeValue("href", "").Replace("&amp;", "&");
                createCDataSection(ele, h);            

            }
        }
        /********************************************************************************************
         * 
         * 
         * ******************************************************************************************/
        protected void dump(String text)
        {
            System.Diagnostics.Debug.WriteLine(text);
        }
        

        public XmlDocument processHTMLDocument(HAP.HtmlDocument doc)
        {
            curDoc = null;
            XmlElement root = createNote("Items", null);


            //找NextPage
            HAP.HtmlNodeCollection nextNodes = doc.DocumentNode.SelectNodes("//a[@class='page-next']");
            if (nextNodes != null)
            {
                //最前面的 next-page 跟最後面的nextPage是一樣的
                HAP.HtmlNode pageNextNode = nextNodes.Last(); //取第二個 
                /**
                 * 而控制哪一個的參數
                 * /search?q=%C4%A7%ABF&initiative_id=tbindexz_20120526&commend=all&sort=sale-desc&ssid=s5-e&s=40#J_FilterTabBar
                 * /search?q=%C4%A7%ABF&initiative_id=tbindexz_20120526&commend=all&sort=sale-desc&ssid=s5-e&s=200#J_FilterTabBar
                 * 是後面的  s=???  
                 * 中間可能會需要虛假的load一兩個
                 */
                XmlElement pageNext = createNote("page-next", root);
                String next = pageNextNode.GetAttributeValue("href", "").Replace("&amp;", "&");                
                
                    
                createCDataSection(pageNext, next);            
            }

            HAP.HtmlNodeCollection items = doc.DocumentNode.SelectNodes("//li[@class='list-item']");
            if (items != null)
            {
                int c = 0;
                foreach (HAP.HtmlNode n in items)
                {
                    c++;
                    dump("item:" + c);
                    XmlElement list_item = createNote("list-item", root);
                    HAP.HtmlNode summary = n.SelectSingleNode(n.XPath+"/h3[@class='summary']");
                    if (summary == null)
                        continue;
                //   dump(n.OuterHtml);

                    processSummary(summary, list_item);                    //title
                    HAP.HtmlNode photo = n.SelectSingleNode(n.XPath + "/div[@class='photo']");
                    if(photo==null)
                        photo = n.SelectSingleNode(n.XPath + "/div[@class='photo ']");
                    processPhoto(photo, list_item);
                    HAP.HtmlNode attribute = n.SelectSingleNode(n.XPath + "/ul[@class='attribute']");
                    
                    processAttribute(attribute, list_item);

                    processIconArea(n, list_item);

                    HAP.HtmlNode seller = n.SelectSingleNode(n.XPath + "/p[@class='seller lister hCard']");
                    if (seller != null)
                        processSeller(seller, list_item);
                    
                    
                }
            }
            return curDoc;        

        }
        public XmlDocument  processHTML(String srcTxt)
        {
            HAP.HtmlDocument doc=new HAP.HtmlDocument();
            doc.LoadHtml(srcTxt);
            return processHTMLDocument(doc);
        }

        public XmlDocument processURL(String url)
        {
            HtmlAgilityPack.HtmlWeb web = new HAP.HtmlWeb();
            HAP.HtmlDocument doc = web.Load(url);
            return processHTMLDocument(doc);
        }

        public XmlDocument processfile(String fileName)
        {
            HAP.HtmlDocument doc = new HAP.HtmlDocument();
            doc.Load(fileName, System.Text.Encoding.GetEncoding("GB2312"));
            return processHTMLDocument(doc);
        }
    }
}
